package sample;

import java.awt.Color;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import robocode.Robot;
import robocode.ScannedRobotEvent;
import utfpr.si.connection.Client;
import utfpr.si.interfaces.IServidor;

public class RoboScannerA extends Robot {

	private Client cli;
	private IServidor refServer;
	private static final ArrayList<String> robos = new ArrayList<String>(Arrays.asList(
		"sample.RoboA (1)",
		"sample.RoboA (2)",
		"sample.RoboA (3)",
		"sample.RoboA (4)",
		"sample.RoboA (5)",
		"sample.RoboA (6)",
		"sample.RoboA (7)",
		"sample.RoboA (8)",
		"sample.RoboAR (1)"));
	
	public RoboScannerA(){
        System.out.println("iniciou o Robo Scanner");
        try {
            cli = new Client(1313,"localhost");
            cli.initClient();
            refServer = cli.getRefServer();
        } catch (RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
	}

	@Override
	public void run() {
		setBodyColor(Color.YELLOW);
		setScanColor(Color.YELLOW);
		setRadarColor(Color.GREEN);

		while(true){
			turnRadarLeft(360.0);
		}
	}

	@Override
	public void onScannedRobot(ScannedRobotEvent event) {
		double x,y,b;
		if(robos.contains(event.getName()) ){
			b = Math.toRadians(event.getBearing()+this.getHeading());
			x = this.getX() + event.getDistance()*Math.sin(b);
			y = this.getY() + event.getDistance()*Math.cos(b);
			try {
				refServer.updateTable(event.getName(), x, y);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}else if( event.getName().startsWith("sample.RoboAR") ){
			b = Math.toRadians(event.getBearing()+this.getHeading());
			x = this.getX() + event.getDistance()*Math.sin(b);
			y = this.getY() + event.getDistance()*Math.cos(b);
			try {
				refServer.setRefemEnemy(event.getName(), x, y);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

}
